# @Author  : wangyiruo
# @Time    : 2024/12/6 18:33
# @Project：Python70_class
import json

import allure
from loguru import logger

from tools.handle_mysql import HandleMysql
from data.setting import my_db
from tools.hande_replace import replace_mark
#
# assert_data = json.loads(assert_data)
# for k,v in assert_data.items():
#     sql_result = HandleMysql(**my_db).query_data(k)  #得到的是个字典
#     print(sql_result)
#     for i in sql_result.value():
#         assert i == v


# 封装
@allure.step("数据库响应断言")
def db_assert(assert_data):
    if assert_data is None:
        logger.info("数据为空，不需要进行数据库断言")
        return
    assert_data = replace_mark(assert_data)
    assert_data = json.loads(assert_data)
    logger.info(f"原始数据库断言数据为：{assert_data}")
    for k, v in assert_data.items():
        logger.info(f"sql语句是{k}")
        sql_result = HandleMysql(**my_db).query_data(k)  # 得到的是个字典
        print(sql_result)
        for i in sql_result.values():
            logger.info(f"实际的结果是{i}")
            logger.info(f"预期的结果是{v}")
            try:
                assert i == v
            except AssertionError as e:
                raise e


if __name__ == '__main__':
    assert_data = """{"select count(1) from tz_order where order_number = '1834573230439141376'":1,
    "select status from tz_order where order_number = '1834573230439141376'":2}"""
    db_assert(assert_data)